package com.yicoollxing.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.yicoollxing.common.ResponseCode;
import com.yicoollxing.common.ServerResponse;
import com.yicoollxing.dao.mapper.FoodMapper;
import com.yicoollxing.dao.mapper.OrderMapper;
import com.yicoollxing.dao.mapper.TakeOutOrderMapper;
import com.yicoollxing.pojo.Order;
import com.yicoollxing.pojo.TakeOutOrder;
import com.yicoollxing.service.IFoodService;
import com.yicoollxing.service.IOutOrderService;
import com.yicoollxing.util.DateTimeUtil;
import com.yicoollxing.vo.FoodDetialVo;
import com.yicoollxing.vo.OrderDetialVo;
import com.yicoollxing.vo.OrderListVo;
import com.yicoollxing.vo.OutOrderDetailVo;
import com.yicoollxing.vo.OutOrderListVo;

@Service("iOutOrderService")
public class OutOrderServiceImpl implements IOutOrderService{
	@Autowired
	private TakeOutOrderMapper takeOutOrderMapper;

	@Autowired
	private FoodMapper foodMapper;

	@Autowired
	private IFoodService iFoodService;

	public ServerResponse list(Integer pageNum,Integer pageSize,Integer mid) {
		if (mid == null) {
			return ServerResponse.createByErrorCodeMessage(
					ResponseCode.ILLEGAL_ARGUMENT.getCode(),
					ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}

		List<TakeOutOrder> takeOutList = takeOutOrderMapper.selectByMid(mid);
		List<OutOrderListVo> outOrderListVos = Lists.newArrayList();
		for (TakeOutOrder order : takeOutList) {
			outOrderListVos.add(assembleOutOrderList(order));
		}
		PageHelper pageHelper = new PageHelper();
		pageHelper.startPage(pageNum, pageSize);
		PageInfo pageInfo = new PageInfo(outOrderListVos);

		return ServerResponse.createBySuccess(pageInfo);
	}

	private OutOrderListVo assembleOutOrderList(TakeOutOrder order) {
		OutOrderListVo outOrderListVo = new OutOrderListVo();
		outOrderListVo.setId(order.getId());
		outOrderListVo.setOrderNo(order.getOrderNo());
		outOrderListVo.setPayStatus(order.getPayStatus());
		outOrderListVo.setDiscount(order.getDiscount());
		outOrderListVo.setTotle(order.getTotle());
		outOrderListVo.setSendStatue(order.getSendStatue());
		outOrderListVo.setStatus(order.getStatus());
		outOrderListVo.setCreateTime(new DateTimeUtil().dateToStr(order.getCreateTime()));
		outOrderListVo.setUpdateTime(new DateTimeUtil().dateToStr(order.getUpdateTime()));
		
		String foodIds = order.getFoodids();
		String[] foodIdArray = null;
		List<FoodDetialVo> foodDetailVoList = null;
		if (StringUtils.isNotBlank(foodIds)) {
			foodDetailVoList = Lists.newArrayList();
			foodIdArray = foodIds.split(",");
			for (int i = 0; i < foodIdArray.length; i++) {
				if (StringUtils.isNotBlank(foodIdArray[i])) {
					FoodDetialVo foodDetialVo = (FoodDetialVo) iFoodService
							.getFoodDetail(Integer.valueOf(foodIdArray[i]))
							.getData();
					foodDetailVoList.add(foodDetialVo);
				}
			}
			outOrderListVo.setFoodDetialVoList(foodDetailVoList);
		}
		return outOrderListVo;
	}

	public ServerResponse detail(Integer orderId) {
		if (orderId == null) {
			return ServerResponse.createByErrorCodeMessage(
					ResponseCode.ILLEGAL_ARGUMENT.getCode(),
					ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		TakeOutOrder takeOutOrder = takeOutOrderMapper.selectByPrimaryKey(orderId);
		if(takeOutOrder==null){
			return ServerResponse.createByErrorMessage("订单不存在");
		}
		OutOrderDetailVo orderDetialVo = assembleOutOrderDetailVo(takeOutOrder);

		return ServerResponse.createBySuccess(orderDetialVo);
	}

	private OutOrderDetailVo assembleOutOrderDetailVo(TakeOutOrder order) {
		OutOrderDetailVo outOrderDetailVo = new OutOrderDetailVo();
		outOrderDetailVo.setId(order.getId());
		outOrderDetailVo.setOrderNo(order.getOrderNo());
		outOrderDetailVo.setPayStatus(order.getPayStatus());
		outOrderDetailVo.setDiscount(order.getDiscount());
		outOrderDetailVo.setTotle(order.getTotle());
		outOrderDetailVo.setSendStatue(order.getSendStatue());
		outOrderDetailVo.setStatus(order.getStatus());
		outOrderDetailVo.setCreateTime(new DateTimeUtil().dateToStr(order.getCreateTime()));
		outOrderDetailVo.setUpdateTime(new DateTimeUtil().dateToStr(order.getUpdateTime()));
		outOrderDetailVo.setOurName(order.getOurName());
		outOrderDetailVo.setOutAddress(order.getOutAddress());
		outOrderDetailVo.setOutPhone(order.getOutPhone());
		outOrderDetailVo.setOrderMid(order.getOrderMid());
		outOrderDetailVo.setUserId(order.getUserId());
		outOrderDetailVo.setPayMethod(order.getPayMethod());
		outOrderDetailVo.setVoucherid(order.getVoucherid());
		outOrderDetailVo.setActivatecode(order.getActivatecode());
		String foodIds = order.getFoodids();
		String[] foodIdArray = null;
		List<FoodDetialVo> foodDetailVoList = null;
		if (StringUtils.isNotBlank(foodIds)) {
			foodDetailVoList = Lists.newArrayList();
			foodIdArray = foodIds.split(",");
			for (int i = 0; i < foodIdArray.length; i++) {
				if (StringUtils.isNotBlank(foodIdArray[i])) {
					FoodDetialVo foodDetialVo = (FoodDetialVo) iFoodService
							.getFoodDetail(Integer.valueOf(foodIdArray[i]))
							.getData();
					foodDetailVoList.add(foodDetialVo);
				}
			}
			outOrderDetailVo.setFoodDetialVoList(foodDetailVoList);
		}
		return outOrderDetailVo;
	}

	public ServerResponse serach(Integer orderNo){
		if(orderNo==null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		TakeOutOrder takeOutOrder=takeOutOrderMapper.selectByOrderNo(orderNo);
		OutOrderListVo outOrderListVo=assembleOutOrderList(takeOutOrder);
		return ServerResponse.createBySuccess(outOrderListVo);
	}
	

}
